package io.finer.erp.jeecg.bas.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import io.finer.erp.jeecg.bas.entity.BasMaterial;
import io.finer.erp.jeecg.bas.entity.BasMaterialTemp;
import io.finer.erp.jeecg.bas.mapper.BasMaterialTempMapper;
import io.finer.erp.jeecg.bas.service.IBasMaterialService;
import io.finer.erp.jeecg.bas.service.IBasMaterialTempService;

/**
 * @Description: pda根据分类生成的临时物料
 * @Author: jeecg-boot
 * @Date:   2020-10-17
 * @Version: V1.0
 */
@Service
public class BasMaterialTempServiceImpl extends ServiceImpl<BasMaterialTempMapper, BasMaterialTemp> implements IBasMaterialTempService {
	@Autowired
	private IBasMaterialService materialService;

	@Override
	public BasMaterialTemp getByCode(String code) {
		LambdaQueryWrapper<BasMaterialTemp> query = new LambdaQueryWrapper<>();
		query.eq(BasMaterialTemp::getCode, code);
		BasMaterialTemp temp = this.getOne(query);
		return temp;
	}

	@Override
	public BasMaterial tempToMaterial(String code) {
		BasMaterial material = this.materialService.getMaterialByCode(code);
		if(material == null) {
			BasMaterialTemp temp = this.getByCode(code);
			if(null == temp) {
				return null;
			}
//			String codeImg = this.materialService.createCodeImg(code);
			material = packageBasMaterial(temp);
//			material.setCodeImg(codeImg);
			this.materialService.save(material);
		}
		return material;
	}

	@Override
	public BasMaterial getTemMaterial(String code) {
		BasMaterialTemp temp = this.getByCode(code);
		if(null == temp) {
			String[] codes = StrUtil.split(code, "+");
			if(codes.length > 1) {
				code = codes[codes.length - 1];
				LambdaQueryWrapper<BasMaterialTemp> query = new LambdaQueryWrapper<>();
				query.likeLeft(BasMaterialTemp::getCode, code);
				List<BasMaterialTemp> list = this.list(query);
				if(CollUtil.isNotEmpty(list)) {
					return packageBasMaterial(list.get(0));
				}
			}
		}
		if(null == temp) {
			return null;
		}
		return packageBasMaterial(temp);
	}
	
	private BasMaterial packageBasMaterial(BasMaterialTemp temp) {
		BasMaterial material = new BasMaterial();
		material.setCategoryId(temp.getCategoryId());
		material.setCode(temp.getCode());
		material.setCreateBy(temp.getCreateBy());
		material.setCreateTime(temp.getCreateTime());
		material.setName(temp.getName());
		material.setIsEnabled(1);
		material.setIsStop(0);
		material.setWidth(temp.getWidth());
		material.setLength(temp.getLength());
		material.setUnitId(temp.getUnitId());
		material.setModel(temp.getModel());
		material.setSquare(temp.getSquare());
		material.setPid(temp.getPid());
		material.setFormulaId(temp.getFormulaId());
		material.setRemark(temp.getRemark());
		material.setCustomerId(temp.getCustomerId());
		material.setQuantity(temp.getAmount());
		BigDecimal amount = temp.getAmount();
		if(null != amount) {
			material.setQuantity(amount);
		} else {
			material.setQuantity(temp.getSquare());
		}
		BigDecimal pageNum = temp.getPageNum();
		if(null != pageNum) {
			material.setViceQuantity(pageNum);
		}
		return material;
	}

	@Override
	public List<BasMaterialTemp> getByCodes(List<String> codes) {
		return lambdaQuery().in(BasMaterialTemp::getCode, codes).list();
	}
}
